<template>
  <basic-container>
    <div class="container-body">
      <el-form :inline="true" :model="query" class="demo-form-inline">
        <el-form-item label="姓名">
          <el-input size="small" v-model="query.realname" placeholder="姓名" clearable></el-input>
        </el-form-item>
        <el-form-item label="部门">
          <el-input size="small" v-model="query.deptName" placeholder="部门" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button size="small" type="primary" icon="el-icon-search" @click="onSearch" :loading="loading">搜索
          </el-button>
          <el-button size="small" icon="el-icon-delete" @click="searchReset" :loading="loading">清空</el-button>
        </el-form-item>
      </el-form>
      <div class="btn-operate">
        <el-button @click="showAddView" type="primary" size="small" icon="el-icon-plus">
          新增
        </el-button>
      </div>
      <el-table header-row-class-name="container-table-header" :data="data" size="small" style="width: 100%;margin-bottom: 20px;"
                row-key="id" border stripe v-loading="loading">
        <el-table-column prop="username" label="用户名">
        </el-table-column>
        <el-table-column prop="realname" label="姓名">
        </el-table-column>
        <el-table-column prop="position" label="职称">
          <template slot-scope="scope">
            <DictTag :options="dict.type.ZC" :value="scope.row.position"></DictTag>
          </template>
        </el-table-column>
        <el-table-column prop="phone" label="手机号">
        </el-table-column>
        <el-table-column prop="deptName" label="部门">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button v-if="permissions.audit_user_add" type="text" size="small" icon="el-icon-edit" @click="showEditBox(scope.row)">编辑
            </el-button>
            <el-button v-if="permissions.audit_user_del" type="text" size="small" icon="el-icon-delete" @click="handleDelete(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="container-page">
        <el-pagination
          @size-change="sizeChange"
          @current-change="currentChange"
          :current-page="page.current"
          :page-sizes="[10, 20, 30, 40, 50, 100]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total">
        </el-pagination>
      </div>
    </div>
    <el-dialog :title="formTitle" :visible.sync="formBox" :close-on-click-modal="!formBox" :append-to-body="true" :before-close="resetForm" width="800px" top="5vh">
      <el-form ref="optForm" :model="form" label-width="80px">
        <el-row>
          <el-col :span="11">
            <el-form-item label="姓名" prop="realname" :rules="{required: true, message: '姓名不能为空', trigger: ['blur', 'change']}">
              <el-input v-model="form.realname" placeholder="姓名"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="手机号" prop="phone" :rules="{required: true, message: '手机号不能为空', trigger: ['blur', 'change']}">
              <el-input v-model="form.phone" placeholder="手机号"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetForm" size="small" icon="el-icon-close">关闭</el-button>
        <el-button type="primary" @click="submit" size="small" icon="el-icon-check">保存</el-button>
      </span>
    </el-dialog>
  </basic-container>
</template>

<script>
import {mapGetters} from "vuex";
import {addObj, putObj, fetchList} from "@/api/adis/base/userAudited";
import {delObj, getObj} from "@/api/admin/user";
import {fetchTree, fetchTreeByType} from "@/api/admin/dept";
import Template from "@/views/adis/base/template.vue";

export default {
  directives: {
  },
  components: {
    Template
  },
  dictTypes: ['ZC'],
  data() {
    return {
      importBox: false,
      isEdit: false,
      isView: false,
      viewBox: false,
      formBox: false,
      formTitle: '',
      loading: false,
      query: {},
      form: {
      },
      page: {
        size: 10,
        current: 1,
        total: 0
      },
      data: [],
      selectionRow: [],
      Authorization: '',
      fileList: [], // 文件

      transferDialog: false,
      transferDialogType: '',
      transferDialogArr: [],
      transferDialogMultiple: false,
      transferCurrent: {},

      taskAuditDialog: false,
      taskAuditLogDialog: false,
      processId: '',
      defaultProps: {
        label: "name",
        value: "id"
      },
      treeDeptData: [],

      deptType: 2,
    }
  },
  computed: {
    ...mapGetters(["permissions"])
  },
  created() {
    this.onLoad(this.page);
    this.Authorization = 'Bearer ' + this.$store.getters.access_token
  },
  methods: {
    submit() {
      this.$refs['optForm'].validate((valid) => {
        if (valid) {
          this.loading = true;
          if(this.form.userId) {
            putObj(this.form).then(() => {
              this.$message({
                type: "success",
                message: "操作成功!"
              });
              this.resetForm();
              this.onLoad(this.page);
            }, () => {
              this.loading = false;
            });
          } else {
            addObj(this.form).then(() => {
              this.$message({
                type: "success",
                message: "操作成功!"
              });
              this.resetForm();
              this.onLoad(this.page);
            }, () => {
              this.loading = false;
            });
          }

        } else {
          return false;
        }
      });
    },
    showAddView() {
      this.formTitle = '新增';
      this.formBox = true;
      this.form = {};
      this.getDeptData();
    },
    showEditBox(row) {
      getObj(row.userId).then((res) => {
        this.form = res.data.data;
        this.formTitle = '编辑';
        this.formBox = true;
        this.isView = false;
        this.isEdit = true;
        this.getDeptData();
      });
    },
    showViewBox(row) {
      this.form = {};
      this.form.planId =  row.planId
      this.formTitle = '查看';
      this.viewBox = true;
      this.isView = true;
    },
    closeViewBox() {
      this.viewBox = false;
    },
    handleDelete(row) {
      this.$confirm("确定将选择的数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        return delObj(row.userId);
      })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    resetForm() {
      this.formBox = false;
      this.viewBox = false;
      this.isEdit = false;
      this.isView = false;
      this.form = {};
      this.formTitle = '';
      this.$refs['optForm'] && this.$refs['optForm'].resetFields();
      this.viewBox = false;
      this.printBox = false;
    },
    onSearch() {
      this.page.current = 1;
      this.onLoad(this.page);
    },
    searchReset() {
      this.query = {};
      this.page.current = 1;
      this.onLoad(this.page);
    },
    currentChange(current) {
      this.page.current = current;
      this.onLoad(this.page);
    },
    sizeChange(size) {
      this.page.size = size;
      this.onLoad(this.page);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      fetchList(Object.assign(params, this.query, page)).then(res => {
        this.loading = false;
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
      }, () => {
        this.loading = false;
      });
    },
    handleRemove(file, fileList) {
      this.fileList = fileList
    },
    handleSuccess(response, file, fileList) {
      this.fileList = fileList;
    },
    handlePreview(file) {
      window.open(file.url ? file.url : file.response.data.url)
    },
    beforeRemove(file) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },
    getDeptData() {
      // 查询部门树
      fetchTreeByType({type: this.deptType}).then(response => {
        this.treeDeptData = response.data.data;
      });
    },
  }
};
</script>

<style>
</style>
